package com.course.business.controller.admin;

import com.course.server.domain.Chapter;
import com.course.server.domain.Test;
import com.course.server.dto.ChapterDto;
import com.course.server.dto.ChapterPageDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.R;
import com.course.server.exception.ValidatorException;
import com.course.server.service.ChapterService;
import com.course.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname AController
 * @Description TODO
 * @Date 2023-10-29 17:00
 * @Created by 贾悦栋
 */
@RestController
@RequestMapping("admin")
public class ChapterController {

    public static final String BUSINESS_NAME ="大章";
    @Resource
    private ChapterService chapterService;

    // @PostMapping("/getChapterList")
    // public PageDto<ChapterDto> test(@RequestBody PageDto<ChapterDto> pageDto){
    //     chapterService.getList(pageDto);
    //     return pageDto;
    // }

    /**
     * 获取大章列表
     * @param chapterPageDto
     * @return
     */
    @PostMapping("/getChapterList")
        public R getChapterList(@RequestBody ChapterPageDto chapterPageDto){
            ValidatorUtil.require(chapterPageDto.getCourseId(),"课程Id");
            chapterService.getList(chapterPageDto);
            return R.ok().put("data",chapterPageDto);
        }

    // @PostMapping("/save")
    //     public ChapterDto test(@RequestBody ChapterDto chapterDto){
    //         chapterService.save(chapterDto);
    //         return chapterDto;
    //     }

    /**
     * 保存章节列表信息（新增、编辑）
     * @param chapterDto
     * @return
     */
    @PostMapping("/save")
    public R saveChapter(@RequestBody ChapterDto chapterDto){
        //校验字段，异常会调用ValidatorException
        ValidatorUtil.require(chapterDto.getName(),"名称");
        ValidatorUtil.require(chapterDto.getCourseId(),"课程Id");
        ValidatorUtil.length(chapterDto.getCourseId(),"课程Id",1,8);

        chapterService.save(chapterDto);
        return R.ok();
    }

    /**
     * 删除大章信息
     * @param chapterId
     * @return
     */
    @DeleteMapping("/delete/{chapterId}")
    public R deleteChapterById(@PathVariable String chapterId){
        chapterService.deleteByChapterId(chapterId);
        return R.ok();
    }



}
